
#include "setting_sys_update.h"
#include "img_common.h"
#include "pt_fs.h"
#include "root.h"
#include "sys_font.h"

static ui_setting_sys_update_t  ui_set_sys_up;

// static  char * icon_loading[CFG_MAX_LOADING_NUM] = 
// {
//     "Loading_00.png", "Loading_01.png", "Loading_02.png", "Loading_03.png", "Loading_04.png", 
//     "Loading_05.png", "Loading_06.png", "Loading_07.png", "Loading_08.png", "Loading_09.png", 
//     "Loading_10.png", "Loading_11.png", "Loading_12.png", "Loading_13.png", "Loading_14.png", 
//     "Loading_15.png", "Loading_16.png", "Loading_17.png"
// };


static int setting_sys_status_update(void)
{

    ui_set_sys_up.index++;
    if(ui_set_sys_up.index >= CFG_MAX_LOADING_NUM)
    {
        ui_set_sys_up.index = 0;
    }

    lv_img_set_src(ui_set_sys_up.img_loading, loading_img[ui_set_sys_up.index]);

    return 0;
}


void setting_sys_update_timer_callback(struct _lv_timer_t * t)
{
    setting_sys_status_update();

#if  1 //for test
    static int test_cnt = 0;
    test_cnt ++;
    if(test_cnt >= 30)
    {
        test_cnt = 0;
        //root_send_msg(MSG_SYS_UP_SUCCEED,0,0);
        root_send_msg(MSG_SYS_UP_ERR,0,0,0,NULL);
    }
#endif

}

int ui_setting_sys_update_create(lv_obj_t *superview)
{
    char txt_buff[24];

    ui_set_sys_up.index = 0;
    ui_set_sys_up.update_timer = NULL;
    //**********************************************************************************************
    //title 

    ui_set_sys_up.title = lv_label_create(superview);
    lv_obj_set_pos(ui_set_sys_up.title,24,43);
    lv_obj_set_size(ui_set_sys_up.title,432,40);
    lv_obj_set_style_text_align(ui_set_sys_up.title,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_sys_up.title,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_sys_up.title,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_sys_up.title, lv_font_get(28), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_sys_up.title, "更新系统中");
    //**********************************************************************************************
    //loading 

    ui_set_sys_up.img_loading = lv_img_create(superview);
    lv_img_set_src(ui_set_sys_up.img_loading, loading_img[0]);
    lv_obj_set_pos(ui_set_sys_up.img_loading,208,110);
    //**********************************************************************************************
    //tip

    ui_set_sys_up.tip = lv_label_create(superview);
    lv_obj_set_pos(ui_set_sys_up.tip,24,196);
    lv_obj_set_size(ui_set_sys_up.tip,432,40);
    lv_obj_set_style_text_align(ui_set_sys_up.tip,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_sys_up.tip,LV_OPA_40,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_sys_up.tip,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_sys_up.tip, lv_font_get(20), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_sys_up.tip, "更新过程中设备会自动重启，请务断电");
    //**********************************************************************************************
    //update timer

    if(!ui_set_sys_up.update_timer){
        ui_set_sys_up.update_timer = lv_timer_create(setting_sys_update_timer_callback,33,NULL);
    }else {
        lv_timer_reset(ui_set_sys_up.update_timer);
    }
    //**********************************************************************************************

    return 0;
}

int ui_setting_sys_update_destroy(lv_obj_t *superview)
{
    ui_set_sys_up.index = 0;

    if(ui_set_sys_up.title){
        lv_obj_del(ui_set_sys_up.title);
        ui_set_sys_up.title = NULL;
    }
    if(ui_set_sys_up.img_loading){
        lv_obj_del(ui_set_sys_up.img_loading);
        ui_set_sys_up.img_loading = NULL;
    }
    if(ui_set_sys_up.tip){
        lv_obj_del(ui_set_sys_up.tip);
        ui_set_sys_up.tip = NULL;
    }
    if(ui_set_sys_up.update_timer){
        lv_timer_del(ui_set_sys_up.update_timer);
        ui_set_sys_up.update_timer = NULL;
    }

    return 0;
}

static void setting_sys_update_on_view_did_load(page_base_t* self)
{
    ui_setting_sys_update_create(self->root);
}

static void setting_sys_update_on_view_did_unload(page_base_t* self)
{
    ui_setting_sys_update_destroy(self->root);
}


static page_vtable_t setting_sys_update_param = {
    .on_custom_attr_config  = NULL,
    .on_view_load           = NULL,
    .on_view_did_load       = setting_sys_update_on_view_did_load,
    .on_view_will_appear    = NULL,
    .on_view_did_appear     = NULL,
    .on_view_will_disappear = NULL,
    .on_view_did_disappear  = NULL,
    .on_view_did_unload     = setting_sys_update_on_view_did_unload,
};


page_vtable_t* lv_page_setting_sys_update_create(void)
{
    return &setting_sys_update_param;
}
